package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketBrand;
import com.cskaoyan.bean.po.MarketBrandExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.service.adminservice.BrandService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:J
 * @datetime 2022/12/29 22:13
 * @description: TODO:品牌制造商相关 list create update delete
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    MarketBrandMapper marketBrandMapper;

    /**
     * 展示页
     *
     * @return
     */
    @Override
    public CommonData<MarketBrand> list(BasePageInfo info,Integer id,String name) {
        //开启分页
        PageHelper.startPage(info.getPage(),info.getLimit());

        //根据id查询数据，id不会为空，不校验。

        MarketBrandExample example = new MarketBrandExample();
        //排序
        example.setOrderByClause(info.getSort()+" "+info.getOrder());
        //建查询标准(条件)
        MarketBrandExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
       // criteria.andIdIsNotNull();
        criteria.andDeletedEqualTo(false);
        if (id != null){
            criteria.andIdEqualTo(id);
        }
        List<MarketBrand> list = marketBrandMapper.selectByExample(example);

        //获得分页信息
        //data:limit,list,page,pages,total
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 增加品牌
     * 目前只发现了底价必须是数字，各值可以相同，不需要先查再建，图片可以不传，其他三项必填
     * 判空
     * 401,"参数不对"
     *
     * @param marketBrand
     * @return
     */
    @Override
    public BaseRespVo create(MarketBrand marketBrand) {
        BaseRespVo baseRespVo = new BaseRespVo<>();
        //String desc,String name,BigDecimal floorPrice,String picUrl
        //正则："^[0-9]*$" 正则好像是字符串校验啊？
        //TODO
        try {
            Boolean a = (marketBrand.getFloorPrice().toString().matches("\"^[5-9]*$\" "));
            //底价不全为数字
            if (a == false) {
                baseRespVo.setErrno(555);
                baseRespVo.setErrmsg("底价为5-9的数字");
            }
            //除图片外其他三项非空
            if (StringUtil.isEmpty(marketBrand.getDesc())
                    || StringUtil.isEmpty(marketBrand.getName())) {
//                || marketBrand.getFloorPrice().equals(null)){
                //TODO:equals(null）？？怎么对BigDecimal判空？
                baseRespVo.setErrno(401);
                baseRespVo.setErrmsg("参数不对");
            }
            //TODO：问问能不能在MarketBrand上加全参构造方法？好像也不用？
            //MarketBrand newMarketBrand=new MarketBrand(marketBrand.getName(),marketBrand.getDesc(),marketBrand.getFloorPrice(),marketBrand.getPicUrl());
            MarketBrand newMarketBrand = new MarketBrand();
            newMarketBrand.setDesc(marketBrand.getDesc());
            newMarketBrand.setName(marketBrand.getName());
            newMarketBrand.setFloorPrice(marketBrand.getFloorPrice());
            newMarketBrand.setPicUrl(marketBrand.getPicUrl());
            //询问一下insertSelective是否是指插入部分值即可
            int affectedRows = marketBrandMapper.insertSelective(newMarketBrand);
            if (affectedRows == 1) {
                baseRespVo.setErrno(0);
                baseRespVo.setData(newMarketBrand);
                baseRespVo.setErrmsg("成功");
            }
        } catch (Exception e) {
            //有一个底价非空不知道怎么校验，试试
            baseRespVo.setErrno(401);
            baseRespVo.setErrmsg("参数不对");
        }
        return baseRespVo;
    }

    @Override
    public BaseRespVo update(MarketBrand marketBrand) {
        BaseRespVo baseRespVo = new BaseRespVo<>();
        Boolean a = marketBrand.getFloorPrice().toString().matches("\"^[0-9]*$\" ");
        //底价不全为数字
        if (a == false) {
            baseRespVo.setErrno(401);
            baseRespVo.setErrmsg("参数不对");
        }

        MarketBrand newMarketBrand = new MarketBrand();
        newMarketBrand.setName(marketBrand.getName());
        newMarketBrand.setPicUrl(marketBrand.getPicUrl());
        newMarketBrand.setDesc(marketBrand.getDesc());
        newMarketBrand.setFloorPrice(marketBrand.getFloorPrice());
        newMarketBrand.setId(marketBrand.getId());
        newMarketBrand.setAddTime(marketBrand.getAddTime());
        newMarketBrand.setSortOrder(marketBrand.getSortOrder());
        newMarketBrand.setDeleted(marketBrand.getDeleted());

        marketBrandMapper.updateByPrimaryKeySelective(newMarketBrand);
        //更新之后用主键查一下整条记录，，，？
        MarketBrand data = marketBrandMapper.selectByPrimaryKey(newMarketBrand.getId());
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(data);

        return baseRespVo;
    }

    @Override
    public BaseRespVo delete(MarketBrand marketBrand) {
        BaseRespVo baseRespVo = new BaseRespVo<>();
//        int affectedRows=marketBrandMapper.deleteByPrimaryKey(marketBrand.getId());
//        //delete返回值是影响行数吗？不记得了=-=
//        if(affectedRows==1){
//            baseRespVo.setErrno(0);
//            baseRespVo.setErrmsg("成功");
//        }
        marketBrand.setDeleted(true);
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }
}
